// pages/userlogin/login.js
const getUserInfoUrl = require('../../utils/config').getUserInfoUrl;
const loginByPhonenumber = require('../../utils/config').loginByPhonenumberUrl;
const getPhoneNumberUrl  = require('../../utils/config').getPhoneNumberUrl;
const loginByMobileUrl   = require('../../utils/config').loginByMobileUrl;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //确认授权
  getPhoneNumber: function (e) {
    var that = this;
     // 拒绝授权
     if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
	    wx.showModal({
	    title: '提示',
	    showCancel: false,
	    content: '未授权您将无法登陆',
	    success: function (res) { }
	    })
    } else{
      var iv = e.detail.iv;
      var encryptedData = e.detail.encryptedData;
     
      //获取手机号
      var phonenumber = that.parsePhoneNumber(encryptedData,iv);
            //手机号登录
            
　　　
    }
    




  },


  //授权后获取用户信息
  loadUserInfo: function () {
    
    
  },
  parsePhoneNumber:function(encryptedData,iv){
    var that = this;
    wx.login({
      success: function (r) {
        var code = r.code; //登录凭证  
        if (code != ' ') {
          //2、调用获取用户信息接口  
          wx.request({
            //自己的服务接口地址 
            url: getUserInfoUrl,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              
              code: code
            },
            success: function (res) {
              
              if (res.data.code == 200) {
                //4.解密成功后 获取自己服务器返回的结果
                //将用户信息缓存到本地
                var openid =  res.data.data.openid;
                var sessionKey = res.data.data.sessionKey;
                wx.setStorageSync("openId", openid);
                that.doParsePhoneNumber(openid,sessionKey,encryptedData,iv);

              } 
            },
            fail: function () {
              wx.showToast({
                title: '系统错误',
                image: '/images/erron.png',
                duration: 2000,
              })
             
            }
          })
        } else {
          
        }
      },
      fail: function () {
        
      }
    })
  },
  doParsePhoneNumber:function(openid,sessionKey,encryptedData,iv){
    wx.request({
      url: getPhoneNumberUrl,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        encryptedData: encryptedData,
        iv: iv,
        openId: openid,
        sessionKey: sessionKey
      },
      success: function (res) {
        
        if (res.data.code == 200) {
          var mobile = res.data.data;
          //登录
          wx.request({
            url: loginByMobileUrl,
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            data: {
              mobile: mobile
            },
            success: function (res) {
             
              if (res.data.code == 200) {
                //登录
                var token = res.data.data;
                wx.setStorageSync("token", token);
                wx.showToast({
                  title: '登录成功',
                  success: function(res){
                    wx.switchTab({
                      url: '../../pages/index/index',
                    });
                  },
                })
               
              }
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  accountLogin(){
    wx.redirectTo({
      url: '/pages/login/login'
    })
  }


})